package leetcode.code0703;

import java.util.PriorityQueue;

public class KthLargest {

	PriorityQueue<Integer> queue;
	int k;

	public KthLargest(int k, int[] nums) {
		queue = new PriorityQueue<>((a, b) -> a - b);
		for (int num : nums) {
			queue.add(num);
			if (queue.size() > k) {
				queue.poll();
			}
		}
		this.k = k;
	}

	public int add(int val) {
		queue.add(val);
		if (queue.size() > k) {
			queue.poll();
		}
		return queue.peek();
	}
}
